Skip to content

Fix Decimal type when precision and scale are unspecified #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2021
Merged

Fix Decimal type when precision and scale are unspecified #197

merged 1 commit into from
Apr 29, 2021

Conversation

carlallen
Copy link
Contributor

@carlallen carlallen commented Apr 23, 2021

The changes to make intervals work broke decimal fields that do not specify precision and scale. #176

Copy link
Contributor

@keithdoggett keithdoggett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this! The fix and tests look good, I just left a few notes for you to take a look at.

Comment on lines -250 to -277
# TODO(#178) this should never use DecimalWithoutScale since scale
# is assumed to be 0 if it is not explicitly defined.
#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment should still be here since ideally we wouldn't need this conditional and OID::Decimal would properly handle the case where precision is defined, but the scale is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed it because it's not in the original rails code and the new logic is much closer to the original logic in rails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I think issue #178 can be closed since this handles the case described in that issue (DECIMAL where only precision is specified).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can you add this back in now? I'm going to add to that issue to reflect the problem we saw with precision being improperly handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified it does the same behavior in vanilla activerecord.

Comment on lines 12 to 19
setup do
@connection = ActiveRecord::Base.connection
@connection.create_table("postgresql_numbers", force: true) do |t|
t.decimal 'decimal_default'
t.decimal 'decimal_with_p', precision: 5
t.decimal 'decimal_with_p_and_s', precision: 5, scale: 4
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the indentation's off here.

@keithdoggett keithdoggett mentioned this pull request Apr 26, 2021
Comment on lines 26 to 28
record = PostgresqlNumber.new(decimal_default: 111.222, decimal_with_p: 111.222, decimal_with_p_and_s: 111.222)
assert_equal record.decimal_default, 111.222
assert_equal record.decimal_with_p, 11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is failing because the ActiveRecord Types don't handle precision properly. Both Type::DecimalWithoutScale and OID::Decimal allow any number of digits to the left of the decimal point for some reason. I can look into it more to see if using the limit parameter helps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can just drop that test since it's not as important, it's really only the first assert that I'm actually worried about here.

@keithdoggett
Copy link
Contributor

I think this is good now. I'll add to #178 to reflect the issue where precision is not handled correctly by the ActiveRecord types and we'll keep that comment in there to track it.

@rafiss what are your thoughts on this? This patch fixes the case where a decimal column is created without specifying precision or scale, but there are still inconsistencies between ActiveRecord's decimal implementation and CockroachDBs implementation. We talked about this before and the conclusion was to keep everything consistent with ActiveRecord and just track it in case there are changes.

Also once the record is created in the database, it should be properly handled and subsequent reads from the db will have the correct precision and scale, but as we saw from the failing test it does create issues before then.

@rafiss
Copy link
Contributor

rafiss commented Apr 29, 2021

@keithdoggett thanks for reviewing and your explanation!

@carlallen would you mind rebasing on top of the latest changes from master and squashing this into one commit with a proper message? then i will merge this

@carlallen carlallen changed the title Decimal Columns are broken Fix Decimal type when precision and scale are unspecified Apr 29, 2021
@carlallen
Copy link
Contributor Author

Rebased and squashed with a more informative message

@rafiss
Copy link
Contributor

rafiss commented Apr 29, 2021

Thanks for finding this issue and fixing it @carlallen! Just waiting for CI to pass, then I'll merge

@rafiss rafiss merged commit 57c0729 into cockroachdb:master Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants